import send_msg from '@feidao/service/send-msg';

export interface ICountParam {
	criteria: object;
	query_mode: 1;
	sort: {};
	spaceid: string;
	// sql: string;
	systemid: string;
	tablename: string;
}

interface ICountResult {
	query_mode: 1;
	spaceid: string;
	systemid: string;
	tablename: string;
	count: number;
}

export default function count(action_id: string, session_id: string, spaceid: string, systemid: string) {
	const params = [] as ICountParam[];

	function prepare(tablename: string, filter: object) {
		params.push({
			criteria: filter,
			query_mode: 1,
			sort: {},
			spaceid,
			systemid,
			tablename
		});
	}

	async function exec() {
		const data = await (send_msg(action_id, session_id, spaceid)('dbacc.count.intranet', params) as Promise<ICountResult[]>);
		return data.map((d) => {
			return d.count;
		});
	}

	return {
		exec,
		prepare
	};
}
